package controllers.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JOptionPane;

import controllers.shared.GetInputLoop;

import models.LexiconFileModel;
import models.LexiconModel;
import models.OntologyModel;
import objects.ConceptList;
import objects.Lexicon;
import objects.LexiconList;

/* Creates a new language */
/* actionPerformed when you click the menu "PROGRAM" and then click the menu item "New Language" */
public class MenuNewLanguageActionListener implements ActionListener {
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String input = null;
		
		try {
			GetInputLoop.setInputDialog("Enter the new language to create:", "Create New Target Language", JOptionPane.OK_CANCEL_OPTION);
			GetInputLoop.setErrorDialog("Please enter the name of the new language.", "Error");
			
			input = GetInputLoop.getInput();
			
			if (input == null) {
				return;
			}
			
			/** Put data into data structure **/
			
			Lexicon lexicon = new Lexicon();
			
			for (ConceptList concept : OntologyModel.getOntology()) {
				LexiconList lexiconList = new LexiconList();
				lexiconList.setLabel(concept.getLabel());
				lexicon.addLexiconListItem(lexiconList);
			}
			
			LexiconModel.setLexicon(lexicon);
			
			/* Create new file for language */
			LexiconFileModel.setFile(new File("src/xml/languages/" + input.toLowerCase() + ".xml"));
			LexiconModel.write();
		}
		catch (Exception ex) {}
	}
	
}
